import 'dart:developer';

import 'package:lighthouse/index.dart';

class HintWidget extends StatelessWidget {
  PageData? pageData;

  HintWidget({
    Key? key,
    this.pageData,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    if (pageData == null) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Image.asset('images/hint_text.png'),
            Padding(
              padding: EdgeInsets.all(10.0),
              child: Text(
                '暂无数据',
                style: themeData.textTheme.labelMedium
                    ?.copyWith(color: themeData.colorScheme.onSecondary),
              ),
            )
          ],
        ),
      );
    } else {
      if (pageData!.loadStatus == LoadStatus.loading) {
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              CircularProgressIndicator(
                strokeWidth: 1.0,
              ),
              Padding(
                padding: EdgeInsets.all(10.0),
                child: Text(
                  '加载中...',
                  style: themeData.textTheme.labelMedium
                      ?.copyWith(color: themeData.colorScheme.onSecondary),
                ),
              )
            ],
          ),
        );
      } else if (pageData!.loadStatus == LoadStatus.loadFail) {
        switch (pageData!.errorType) {

          /// It occurs when url is opened timeout.
          case DioErrorType.connectTimeout:
          case DioErrorType.sendTimeout:
          case DioErrorType.receiveTimeout:
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Image.asset('images/hint_wifi.png'),
                  Padding(
                    padding: EdgeInsets.all(10.0),
                    child: Text(
                      '网络超时',
                      style: themeData.textTheme.labelMedium
                          ?.copyWith(color: themeData.colorScheme.onSecondary),
                    ),
                  )
                ],
              ),
            );

          /// When the server response, but with a incorrect status, such as 404, 503...
          case DioErrorType.response:
            if (pageData!.code == 401) {
              return Center(
                child: ElevatedButton(
                    onPressed: () {
                      Navigator.pushReplacementNamed(context, "login");
                    },
                    child: Text(S.of(context).login)),
              );
            } else if (pageData!.code == 503) {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Image.asset('images/hint_error.png'),
                    Padding(
                      padding: EdgeInsets.all(10.0),
                      child: Text(
                        '服务器繁忙，请稍后再试！',
                        style: themeData.textTheme.labelMedium
                            ?.copyWith(color: themeData.colorScheme.onSecondary),
                      ),
                    )
                  ],
                ),
              );
            } else {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Image.asset('images/hint_error.png'),
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: Text(
                        pageData!.hint,
                        style: themeData.textTheme.labelMedium
                            ?.copyWith(color: themeData.colorScheme.onSecondary),
                      ),
                    )
                  ],
                ),
              );
            }

          /// When the request is cancelled, dio will throw a error with this type.
          case DioErrorType.cancel:
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Image.asset('images/hint_message.png'),
                  Padding(
                    padding: EdgeInsets.all(10.0),
                    child: Text(
                      "请求取消",
                      style: themeData.textTheme.labelMedium
                          ?.copyWith(color: themeData.colorScheme.onSecondary),
                    ),
                  )
                ],
              ),
            );

          /// Default error type, Some other Error. In this case, you can
          /// use the DioError.error if it is not null.
          case DioErrorType.other:
          default:
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Image.asset('images/hint_error.png'),
                  Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: Text(
                      pageData!.hint,
                      style: themeData.textTheme.labelMedium
                          ?.copyWith(color: themeData.colorScheme.onSecondary),
                    ),
                  )
                ],
              ),
            );
        }
      } else {
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Image.asset('images/hint_text.png'),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: Text(
                  pageData!.hint,
                  style: themeData.textTheme.labelMedium
                      ?.copyWith(color: themeData.colorScheme.onSecondary),
                ),
              )
            ],
          ),
        );
      }
    }
  }
}
